<!-- 柱状图 -->
<template>
  <div>
    <!-- <div class="title"><p>{{title}}</p></div> -->
    <div ref="barchart" :style="{ height: height, width: width }"></div>
  </div>
</template>

<script>
import { formatDuration } from "@/utils/index.js";
export default {
  data() {
    return {};
  },
  props: {
    title: {
      type: String,
      default: '按日统计趋势'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '350px'
    },
    chartData: {
      type: Object,
      required: true
    }
  },
  watch: {
    chartData: {
      deep: true,
      handler(val) {
        this.setOptions(val)
      }
    }
  },
  //生命周期 - 创建完成（访问当前this实例）
  created() { },
  //生命周期 - 挂载完成（访问DOM元素）
  mounted() {
    this.myChart = this.$echarts.init(this.$refs.barchart, 'macarons')
    this.setOptions(this.chartData)
  },
  methods: {
    setOptions(chartData) {
      this.myChart.setOption({
        xAxis: {
          type: 'category',
          data: chartData.xAxis
        },
        yAxis: {
          type: 'value',
          axisLabel: {
            formatter: function (value) {
              return `${formatDuration(value)}`;
            }
          }
        },
        legend: {
          data: ['活跃时长', '屏保时长', '锁屏时长', '离开时长'],
          bottom: 10,
          //  selectedMode:false
        },
        tooltip: {
          trigger: "item",
          backgroundColor: '#fff',
          textStyle: {
            color: '#000'
          },
          formatter: function (param) {
            return `${param.marker}${param.name}<b style="margin-left:20px">${param.seriesName}(${formatDuration(param.value)})</b>`
          }
        },
        series: [
          {
            name: "活跃时长",
            data: chartData.t1,
            type: 'bar',
            stack: "stack",
            barWidth: 50,
            itemStyle: {
              color: '#67C23A'
            }
          },
          {
            name: "屏保时长",
            data: chartData.t2,
            type: 'bar',
            stack: "stack",
            barWidth: 50,
            itemStyle: {
              color: '#E6A23C'
            }
          },
          {
            name: "锁屏时长",
            data: chartData.t3,
            type: 'bar',
            stack: "stack",
            barWidth: 50,
            itemStyle: {
              color: '#F56C6C'
            }
          },
          {
            name: "离开时长",
            data: chartData.t4,
            type: 'bar',
            stack: "stack",
            barWidth: 50,
            itemStyle: {
              color: '#909399'
            },
            label: {
                show: true,
                position: 'top',
                formatter: function (params) {
                    // 手动计算当前类目下所有 series 的总和
                    const index = params.dataIndex;
                    const total =
                    chartData.t1[index] +
                    chartData.t2[index] +
                    chartData.t3[index] + 
                    chartData.t4[index];
                    return formatDuration(total);
                }
            }
          },
        ]
      })
      this.myChart.off('click');
      this.myChart.on('click', (params) => {
        this.$emit('barclick', params.name, this.title)
      });
    }
  }
};
</script>
<style scoped></style>
